package communication;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class FileSenderConnection {

	private ClientConnection client; 

	public FileSenderConnection(InetAddress address, int port) throws IOException {

		client = new ClientConnection(address, port);

	}

	public void sendFile(String fileName)
	{

		try {

			//Open up designated file
			File f = new File (fileName);

			//create new input stream from the file
			BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(f));

			//Output it through the receiving end.
			BufferedOutputStream buffOut = new BufferedOutputStream (client.getOutputStream()); 

			DataOutputStream out = new DataOutputStream(client.getOutputStream());
			out.writeUTF(f.getName());



			byte [] b = new byte[4096];
			int bytesRead;

			//While there's something left to read, keep sending bytes.
			while ((bytesRead = buffIn.read(b)) != -1) 
			{
				buffOut.write(b, 0, bytesRead);
			}
			buffOut.close();
			buffIn.close();

		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}
